import {MockMethod} from "vite-plugin-mock";
import {noticeList} from "./data";

export default [
    // 获取数据
    {
        url: '/noticeList',
        method: 'GET',
        timeout: 985,
        response: {
            status: 200,
            data: noticeList['notices'],
            message: "success",
            timestamp: Date.now(),
            total: noticeList['notices'].length,
        }
    },
    // 添加
    {
        url: '/addNotice',
        method: 'POST',
        timeout: 211,
        response: ({body}) => {
            const item = noticeList['notices'].find(item => item.id === body.id)
            if (!item) {
                noticeList['notices'].push({...body})
                return {
                    status: 200,
                    message: "添加成功"
                }
            } else {
                return {
                    status: 401,
                    message: "添加失败"
                }
            }
        }
    },
    // 编辑
    {
        url: '/updateNotice/:id',
        method: 'PUT',
        timeout: 211,
        response: ({query, body}) => {
            const index = noticeList['notices'].findIndex(item => item.id === Number(query.id))
            if (index !== -1) {
                Object.assign(noticeList['notices'][index], body)
                return {
                    status: 200,
                    message: "编辑成功",
                    typeofId: typeof query.id, // string
                }
            }
        }
    },
    // 删除
    {
        url: '/deleteNotice/:id',
        method: "DELETE",
        timeout: 211,
        response: ({query}) => {
            const index = noticeList['notices'].findIndex(item => item.id === Number(query.id))
            if (index !== -1) {
                noticeList['notices'].splice(index, 1)
                return {
                    status: 200,
                    message: '删除成功',
                }
            }
        }
    },

] as MockMethod[]